package com.dhecp.project.finance.controller;

import com.dhecp.common.annotation.Log;
import com.dhecp.common.annotation.RepeatSubmit;
import com.dhecp.common.core.domain.AjaxResult;
import com.dhecp.common.core.page.TableDataInfo;
import com.dhecp.common.enums.BusinessType;
import com.dhecp.common.utils.MessageUtils;
import com.dhecp.common.utils.TypeUtils;
import com.dhecp.core.constant.ModuleTable;
import com.dhecp.framework.util.ShiroUtils;
import com.dhecp.project.common.controller.BaseModuleController;
import com.dhecp.project.common.domain.ModuleNidAndBid;
import com.dhecp.project.common.service.ModuleNidAndBidService;
import com.dhecp.project.finance.domain.OperateExpenseTransfer;
import com.dhecp.project.finance.service.OperateExpenseTransferService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.*;

/**
 * @program: kjecp
 * @description: 公司费用划拨
 * @author: hlf
 * @create: 2023-06-12 16:30
 */
@Controller
@RequestMapping("/finance/operateexpensetransfercw")
public class OperateExpenseTransferCWController extends BaseModuleController {

    private String prefix = "finance/operateexpensetransfercw";
    private String moduleTable = ModuleTable.OPERATE_EXPENSE_TRANSFER_CW;
    private String moduleTableGroup = ModuleTable.OPERATE_EXPENSE_TRANSFER_GROUP;


    @Autowired
    private OperateExpenseTransferService businessService;

    @Autowired
    private ModuleNidAndBidService moduleNidAndBidService;

    /**
     * 打开制单页
     * @Param: [params]  nid 必要参数：单据编码（新增时传默认值 0 ，修改、查看时传单据实际值）
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("finance:operateexpensetransfercw:view")
    @GetMapping("/edit")
    @ResponseBody
    public Map<String, Object> editPage(@RequestParam Map<String, Object> params){
        if (params.get("nid") == null || params.get("nid") == ""){
            params.put("nid", "0");
        }
        params.put("moduleTable", moduleTable);
        params.put("entity", new OperateExpenseTransfer()); //用于获取实体属性名取代对应字典字段名，解决大小写敏感问题
        params.put("moduleUrl", prefix); //页面上级路径
        params.put("prefix", prefix + "/operateexpensetransfer_edit"); //页面路径

        try{
            Map<String, Object> headData = new HashMap<String, Object>();		//表头的业务数据
//          SysModuleSet moduleSet = sysModuleSetService.queryModuleSet(params);//单据设置信息
            OperateExpenseTransfer businessData = businessService.queryFirstRecord(params);
            if (businessData == null){ //未查询到对应单据，页面初始化值
                ModuleNidAndBid moduleNidAndBid = moduleNidAndBidService.queryModuleNidAndBidInitial(params);
                OperateExpenseTransfer businessDataNew = new OperateExpenseTransfer();
                businessDataNew.setNid("0");
                businessDataNew.setBid(moduleNidAndBid.getBid());
                businessDataNew.setCreateTime(new Date());
                businessDataNew.setWriteTime(new Date());
                businessDataNew.setvType("运营");
                businessDataNew.setCid(ShiroUtils.getMandatorCid());
                businessDataNew.setCidName(ShiroUtils.getMandatorCidName());
                businessDataNew.setbUser(ShiroUtils.getMandatorId());
                businessDataNew.setbUserName(ShiroUtils.getMandatorName());
                businessDataNew.setbDept(ShiroUtils.getMandatorDeptPath());
                businessDataNew.setbDeptName(ShiroUtils.getMandatorDeptName());
                businessDataNew.setbPosition(ShiroUtils.getMandatorPositionNameDa());
                businessDataNew.setaUser(ShiroUtils.getUserId());
                businessDataNew.setaUserName(ShiroUtils.getUserName());
                businessDataNew.setaDept(ShiroUtils.getUserDeptPath());
                businessDataNew.setaDeptName(ShiroUtils.getUserDeptName());
                businessDataNew.setaPosition(ShiroUtils.getUserPositionNameDa());
                headData = TypeUtils.entityToMap(businessDataNew);
            }else{
                headData = TypeUtils.entityToMap(businessData);
            }
            params.put("headData", headData); //表头的业务数据

            params.put("checkBox", "yes"); //生成表格gridColumns时包含复选
//            ModelAndView mv = this.openEditPage(params);
            Map<String, Object> mv = this.openEditPage(params);
            return mv;
        }catch(Exception e){
//    		return this.gotoModelAndViewErrorPage(this.getExceptionMessage(e));	//简单信息
//            return this.gotoModelAndViewErrorPage(e.getMessage());	//详细信息
            return new HashMap<String, Object>();    //空信息
        }

    }



    /**
     * 制单页 明细数据
     * @Param: [params]  nid 必要参数：当前 单据编码
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions("finance:operateexpensetransfercw:list")
    @GetMapping("/listbynid")
    @ResponseBody
    public TableDataInfo listByNid(@RequestParam Map<String, Object> params){
        if (params.get("nid") == null || params.get("nid") == ""){
            return getDataTable(MessageUtils.message("module.error.lack.nid"));
        }
        params.put("moduleTable", moduleTable);
        List<OperateExpenseTransfer> list = new ArrayList<OperateExpenseTransfer>();
        try {
            list = businessService.queryListByNid(params);
        } catch (Exception e) {
            e.printStackTrace();
            return getDataTable(this.getExceptionMessage(e).toString());
        }
        return getDataTable(list);
    }


    /**
     * 流转
     *
     * @Param: [params]  infoMap   必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params]  infoItems 必要参数：对应实体类的json格式字符串（仅需 guid）
     * @return
     * @author DH-Xxj
     */
    @Log(title = "公司费用划拨", businessType = BusinessType.FLOWGO)
    @PostMapping("/flowgopost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult flowGoSave(@RequestParam Map<String, Object> params) {
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult = businessService.updateFlowGo(params);
        } catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }
    /**
     * 退回
     *  @Param: [params] infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     *  @Param: [params] infoItems 必要参数：对应实体类的json格式字符串（仅需 guid）
     * @return
     * @author DH-Xxj
     */
    @Log(title = "公司费用划拨", businessType = BusinessType.FLOWBACK)
    @PostMapping("/flowbackpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult flowBackSave(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= businessService.updateFlowBack(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

    /**
     * 保存数据
     * @Param: [params]  infoMap 必要参数：非网格字段（包括头部、底部所有显示和隐藏的字段）
     * @Param: [params]  infoItems 必要参数：对应实体类的json格式字符串
     * @return
     * @author DH-Xxj
     */
    @RequiresPermissions(value={"finance:operateexpensetransfercw:add","finance:operateexpensetransfercw:edit","finance:operateexpensetransfercw:delete"},logical= Logical.OR)
    @Log(title = "公司费用划拨", businessType = BusinessType.UPDATE)
    @PostMapping("/editpost")
    @ResponseBody
    @RepeatSubmit
    public AjaxResult editSave(@RequestParam Map<String, Object> params){
        params.put("moduleTable", moduleTable);
        AjaxResult ajaxResult = new AjaxResult();
        try {
            ajaxResult= businessService.saveData(params);
        }catch (Exception e) {
            ajaxResult = AjaxResult.error(this.getExceptionMessage(e));
        }
        return ajaxResult;
    }

}